package generic.gui.util;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.text.MaskFormatter;

import net.sourceforge.jdatepicker.JDateComponentFactory;
import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;

@SuppressWarnings("serial")
public class CalculaDiasEntreDatas extends JDialog {
	
	private JLabel lblDataDeFabricao;
	private JLabel lblDataDeValidade;
	private JButton btnCalcular;
	
	MaskFormatter mf = new MaskFormatter();
	
	
	private JDatePickerImpl dateInicial;
	private JDatePickerImpl dateFinal;
	private Integer numDias;
			
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			new CalculaDiasEntreDatas().setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public CalculaDiasEntreDatas() {
		try {
			setTitle("Calcula datas");
			setResizable(false);
			setBounds(100, 100, 193, 200);
			setLocationRelativeTo(null);
			setModal(true);
			GroupLayout groupLayout = new GroupLayout(getContentPane());
			groupLayout.setHorizontalGroup(
				groupLayout.createParallelGroup(Alignment.TRAILING)
					.addGroup(groupLayout.createSequentialGroup()
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
							.addGroup(groupLayout.createSequentialGroup()
								.addGap(26)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
									.addComponent(getLblDataDeFabricao())
									.addComponent(getLblDataDeValidade())
									.addComponent(getDateFinal(), 0, 0, Short.MAX_VALUE)
									.addComponent(getDateInicial(), GroupLayout.PREFERRED_SIZE, 138, GroupLayout.PREFERRED_SIZE)))
							.addGroup(groupLayout.createSequentialGroup()
								.addGap(43)
								.addComponent(getBtnCalcular(), GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)))
						.addContainerGap(23, Short.MAX_VALUE))
			);
			groupLayout.setVerticalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
					.addGroup(groupLayout.createSequentialGroup()
						.addGap(11)
						.addComponent(getLblDataDeFabricao())
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(getDateInicial(), GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(getLblDataDeValidade())
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(getDateFinal(), GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
						.addGap(18)
						.addComponent(getBtnCalcular())
						.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
			);
			getContentPane().setLayout(groupLayout);
			setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	private JLabel getLblDataDeFabricao() {
		if (lblDataDeFabricao == null) {
			lblDataDeFabricao = new JLabel("Data de Fabrica\u00E7\u00E3o");
		}
		return lblDataDeFabricao;
	}
	
	private JLabel getLblDataDeValidade() {
		if (lblDataDeValidade == null) {
			lblDataDeValidade = new JLabel("Data de Validade");
		}
		return lblDataDeValidade;
	}
	
	private JButton getBtnCalcular() {
		if (btnCalcular == null) {
			btnCalcular = new JButton("Calcular");
			btnCalcular.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (getDateInicial().getJFormattedTextField().getValue() != null &&
							getDateFinal().getJFormattedTextField().getValue() != null) {
						GregorianCalendar calendarInicio = (GregorianCalendar) getDateInicial().getJFormattedTextField().getValue();
						GregorianCalendar calendarFinal = (GregorianCalendar) getDateFinal().getJFormattedTextField().getValue();
						setNumDias( (Integer) (int) ((calendarFinal.getTimeInMillis() - calendarInicio.getTimeInMillis())/1000/60/60/24));
					}
					dispose();
				}
			});
		}
		return btnCalcular;
	}

	public JDatePickerImpl getDateInicial() {
		if (dateInicial == null) {
			dateInicial = new JDatePickerImpl(new JDatePanelImpl(JDateComponentFactory.createDateModel(0)));
			dateInicial.getJFormattedTextField().setHorizontalAlignment(SwingConstants.CENTER);
			//dateInicial.setTextEditable(true);
		}

		return dateInicial;
	}

	public JDatePickerImpl getDateFinal() {
		if (dateFinal == null) {
			dateFinal = new JDatePickerImpl(new JDatePanelImpl(JDateComponentFactory.createDateModel(0)));
			//dateFinal.getJFormattedTextField().setEditable(true);
			dateFinal.getJFormattedTextField().setHorizontalAlignment(SwingConstants.CENTER);
	
		
		
		}
		return dateFinal;
	}

	public Integer getNumDias() {
		return numDias;
	}

	public void setNumDias(Integer numDias) {
		this.numDias = numDias;
	}

}
